/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author 013v
 */
public class DeliveryAddressManager {
 
     public boolean addDelAddress(DeliveryAddress newDelAdd) {
        boolean result = false;

        try {
            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();

            int i = 1;

            String que = "INSERT into deliveryaddressinfo(DAHouseNum, DAStreet, DASubdivision, DACity, DAZip, DACountry, AccountID) VALUES(?,?,?,?,?,?,?)";
             PreparedStatement pstmt =
                    conn.prepareStatement(que);

            pstmt.setString(i++, newDelAdd.getDAHouseNum());
            pstmt.setString(i++, newDelAdd.getDAStreet());
            pstmt.setString(i++, newDelAdd.getDASubdivision());
            pstmt.setString(i++, newDelAdd.getDACity());
            pstmt.setString(i++, newDelAdd.getDAZip());
            pstmt.setString(i++, newDelAdd.getDACountry());
            pstmt.setString(i++, newDelAdd.getAccountID());
          
            pstmt.executeUpdate();
            //close DB connection
            conn.close();

            result = true;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }

        return result;
    }
    
    
     public DeliveryAddress getDel(String usr){
         DeliveryAddress result = null;
        try {
            
            String uid = new UserManager().getAccountIDFromUser(usr);
            
            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();

            String que = "SELECT * FROM deliveryaddressinfo where AccountID=?";
            PreparedStatement pstmt =
                    conn.prepareStatement(que);

            pstmt.setString(1,uid);

            ResultSet rst = pstmt.executeQuery();
            result = new DeliveryAddress();
            while (rst.next()) {
                result.setDAHouseNum(rst.getString("DAHouseNum"));
                result.setDAStreet(rst.getString("DAStreet"));
                result.setDASubdivision(rst.getString("DASubdivision"));
                result.setDACity(rst.getString("DACity"));
                result.setDAZip(rst.getString("DAZip"));
                result.setDACountry(rst.getString("DACountry"));
               }

            conn.close();

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return result;
     }
     
}
